﻿using System.Collections.Generic;
using System.Linq;
using ArmyManager.Items;

namespace ArmyManager.Units
{
	public class Unit
	{
		public enum UnitTypes
		{
			Commander,
			Common,
			Hero
		}

		public string DisplayName { get; private set; }
		public UnitTypes UnitType { get; private set; }
		private List<Item> _items = new List<Item>();
		public List<Item> Items { get { return _items; } }

		private readonly List<Unit> _subUnits = new List<Unit>();
		public List<Unit> SubUnits
		{
			get { return _subUnits; }
		}

		protected Unit() { }

		public Unit(string displayName, UnitTypes unitType)
		{
			DisplayName = displayName;
			UnitType = unitType;
		}

		public Item ReplaceItem(Item newItem)
		{
			Item oldItem = _items.FirstOrDefault(i => i.ItemType == newItem.ItemType) ?? Item.NullItems[newItem.ItemType];
			_items.Remove(oldItem);
			_items.Add(newItem);
			return oldItem;
		}

		public Unit Clone()
		{
			Unit newUnit = CloneInternal();
			newUnit.DisplayName = DisplayName;
			newUnit.UnitType = UnitType;
			newUnit._items = _items.Select(i => i.Clone()).ToList();
			return newUnit;
		}

		protected virtual Unit CloneInternal()
		{
			return new Unit();
		}
	}
}